package com.michale.system.service.impl;

import com.michale.system.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Michale
 * @apiNote  文件上传 业务类
 */
@Slf4j
@Service("uploadService")
public class UploadServiceImpl implements UploadService {
    /**
     * 上传照片
     *
     * @param headerImg 单个照片
     * @param photos    多个照片
     * @return 结果
     */
    @Override
    public int upLoadImages(MultipartFile headerImg, MultipartFile[] photos) {
        try {
            //保存单个文件
            if (!headerImg.isEmpty()) {
                String originalFilename = headerImg.getOriginalFilename();
                headerImg.transferTo(new File("E:\\Michale\\Michale-Boot\\Images\\" + originalFilename));
            }
            //保存多个文件
            if (photos.length > 0) {
                for (MultipartFile photo : photos) {
                    if (!photo.isEmpty()) {
                        String originalFilename = photo.getOriginalFilename();
                        photo.transferTo(new File("E:\\Michale\\Michale-Boot\\Images\\" + originalFilename));
                    }
                }
            }
        } catch (IOException e) {
            return 0;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return 0;

        }
        return 1;
    }
}
